home *** CD-ROM | disk | FTP | other *** search
- // SampleUAM.c
-
- #include <String.h>
- #include <Resources.h>
- #include <A4Stuff.h>
-
- #include "SampleUAM.h"
- #include "AFPPackets.h"
-
- unsigned char commandBuffer[200];
- unsigned char replyBuffer[512];
- StringPtr gAFPVersion;
-
-
-
- StringPtr FigureAFPVersion(AFPSrvrInfo *,ClientUAMCallbackRec *theCallbacks);
-
- //pascal OSErr UAMCall(UAMArgs *theArgs);
- pascal OSErr main(UAMArgs *theArgs)
- {
- EnterCodeResource();
- OSErr error;
- switch(theArgs->command)
- {
- case kUAMOpen:
- error = SampleOpen(theArgs);
- break;
-
- case kUAMPWDlog:
- error = kNotForUs;
- break;
-
- case kUAMLogin:
- error = SampleLogin(theArgs);
- break;
-
- case kUAMVSDlog:
- DebugStr("\pPut up a Volume Select dialog");
- error = noErr;
- break;
-
- case kUAMChgPassDlg:
- error = kNotForUs;
- break;
-
- case kUAMChgPass:
- error = kNotForUs;
- break;
-
- case kUAMGetInfoSize:
- error = kNotForUs;
- break;
-
- case kUAMGetInfo:
- error = kNotForUs;
- break;
-
- case kUAMClose:
- error = noErr;
- break;
-
- default:
- error = kNotForUs;
- break;
- }
- ExitCodeResource();
- return error;
- }
-
-
- long SampleOpen(UAMArgs *theArgs)
-
- {
- gAFPVersion = FigureAFPVersion(theArgs->Opt.open.srvrInfo,theArgs->callbacks);
-
- theArgs->result = kSampleCfg;
- return noErr;
- }
-
-
- OSStatus SampleLogin(UAMArgs *theArgs){
- OSStatus theError = kUAMError;
- Ptr cmd;
- unsigned long cmdSize;
- Handle theUAMName;
- UAMMessage message;
- StringPtr user = theArgs->Opt.auth.userName;
- StringPtr password = theArgs->Opt.auth.password;
-
- //DebugStr("\pin SampleLogin");
-
- if(!gAFPVersion){
- // put up an alert & return userCancelled error
- DebugStr("\pno AFP version");
- return userCanceledErr;
- }
- if(theArgs->callbacks)
- {
- commandBuffer[0] = kFPLogin;
- cmd = (Ptr) &commandBuffer[1];
- memcpy(cmd,(const char *)&gAFPVersion[0],gAFPVersion[0]+1);
- cmd += gAFPVersion[0] + 1;
-
- // get the UAMSTring from the resource
- theUAMName = Get1Resource(kUAMStr,kUAMProtoName);
- if(!theUAMName)
- return ResError(); // really should be something else (this will be wrong if ResLoad is false)
-
- // put it into the command buffer
- HLock(theUAMName);
- memcpy(cmd,(const char *)&((*theUAMName)[0]),(*theUAMName)[0]+1);
- cmd += (*theUAMName)[0]+1;
- HUnlock(theUAMName);
- ReleaseResource(theUAMName);
-
- // copy in the username
- memcpy(cmd,(const char *)&user[0],user[0]+1);
- cmd += user[0]+1;
- if(((UInt32)cmd - (UInt32)commandBuffer) & 0x01) // is this an odd boundry?
- {
- *cmd++ = 0x00; // put in some padding
- }
-
- // copy in the password, only 8 bytes of password
- memcpy(cmd,(const char *)&password[0],8);
- cmd += 8;
-
- // get the cmd buffer size
- cmdSize = (unsigned long)((unsigned long)cmd - (unsigned long)commandBuffer);
-
- message.commandCode = kOpenSession;
- message.cmdBuffer = commandBuffer;
- message.cmdBufferSize = cmdSize;
- message.replyBuffer = nil;
- message.replyBufferSize = 0;
- message.completion = nil;
- message.contextPtr = nil;
-
- //DebugStr("\pabout to make the login call");
-
- theError = theArgs->callbacks->OpenSessionUPP(theArgs->Opt.auth.srvrAddress,nil,&message);
- if(!theError){
- theArgs->sessionRefNum = message.sessionRefNum;
- }
- theError = message.result;
-
- }
- return theError;
-
- }
-
-
-
- StringPtr FigureAFPVersion(AFPSrvrInfo *info,ClientUAMCallbackRec *callbacks)
- {
- struct AFPClientInfo *theClientInfo = nil;
- short index;
- Ptr versBuf;
- UInt32 versBufsize;
- GetClientInfoPtr *fcn;
-
- //DebugStr("\pFigureAFPVersion");
- callbacks->GetClientInfoUPP(kAFPClientInfo,(ClientInfo **)&theClientInfo);
-
- if(theClientInfo){
- // go through the list of AFP versions supported and try to find them
- // in the SrvrInfoBuffer, first match gets it
- versBuf = (Ptr)((UInt32)info + info->fVerCountOffset+1);
- versBufsize = kMaxAFPCommand - info->fVerCountOffset; // the largest size
-
- for(index = 0; index < theClientInfo->fNumAFPVersions; index++){
- if(FindStringInBuf(theClientInfo->fAFPVersionStrs[index],versBuf,versBufsize)){
- return theClientInfo->fAFPVersionStrs[index];
- }
- }
- }
- return nil;
- }
-
- // rather clunky but it works
- Boolean FindStringInBuf(StringPtr string, Ptr buf, UInt32 bufSize)
- {
- Ptr end = buf + bufSize;
- Byte len = string[0] + 1;
- short index;
-
- while((buf < end) && (*buf++ != string[0])) ; // scan for the proper length
-
- if(!(buf < end)){
- return false;
- }
- for(index = 1; (index < len) && (buf > end); index++){
- if(*buf++ != string[index])
- return false;
- }
- if(!(buf < end)){
- return false;
- }
- return true;
- }